using Microsoft.AspNetCore.Mvc;
using PandaWebAPI.Models.ef;
using PandaWebAPI.Services;

namespace PandaWebAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class LikeController : ControllerBase
    {
        private readonly ILikeService _likeService;

        public LikeController(ILikeService likeService)
        {
            _likeService = likeService;
        }

        [HttpGet]
        public async Task<ActionResult<List<Like>>> GetAllLikes()
        {
            var likes = await _likeService.GetAllAsync();
            return Ok(likes);
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<Like>> GetLikeById(Guid id)
        {
            var like = await _likeService.GetByIdAsync(id);
            if (like == null)
                return NotFound();

            return Ok(like);
        }

        [HttpGet("post/{postId}")]
        public async Task<ActionResult<List<Like>>> GetLikesByPostId(Guid postId)
        {
            var likes = await _likeService.GetLikesByPostIdAsync(postId);
            return Ok(likes);
        }

        [HttpGet("comment/{commentId}")]
        public async Task<ActionResult<List<Like>>> GetLikesByCommentId(Guid commentId)
        {
            var likes = await _likeService.GetLikesByCommentIdAsync(commentId);
            return Ok(likes);
        }

        [HttpGet("user/{userId}")]
        public async Task<ActionResult<List<Like>>> GetLikesByUserId(Guid userId)
        {
            var likes = await _likeService.GetLikesByUserIdAsync(userId);
            return Ok(likes);
        }

        [HttpGet("check")]
        public async Task<ActionResult<bool>> CheckUserLike(
            [FromQuery] Guid userId,
            [FromQuery] Guid? postId,
            [FromQuery] Guid? commentId)
        {
            var hasLiked = await _likeService.HasUserLikedAsync(userId, postId, commentId);
            return Ok(hasLiked);
        }

        [HttpPost("toggle")]
        public async Task<ActionResult<bool>> ToggleLike(
            [FromQuery] Guid userId,
            [FromQuery] Guid? postId,
            [FromQuery] Guid? commentId)
        {
            var isLiked = await _likeService.ToggleLikeAsync(userId, postId, commentId);
            return Ok(isLiked);
        }

        [HttpPost]
        public async Task<ActionResult<Like>> CreateLike(Like like)
        {
            try
            {
                var createdLike = await _likeService.CreateAsync(like);
                return CreatedAtAction(nameof(GetLikeById), new { id = createdLike.LikeId }, createdLike);
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpDelete("{id}")]
        public async Task<ActionResult> DeleteLike(Guid id)
        {
            var result = await _likeService.DeleteAsync(id);
            if (!result)
                return NotFound();

            return NoContent();
        }
    }
} 